# Spaghetti plot
library(ggplot2)
ggplot(data,
aes(x = time,
y = strength,
color = factor(trt),
group = id)) +
geom_line(alpha = 0.6) +
stat_summary(fun=mean,
geom="line",
show.legend = F,
lwd=3,
aes(group=trt)) +
# geom_smooth(aes(group = trt), method = "lm", se = TRUE) + #linear trend
labs(title = "Strength Over Time by Treatment Group",
x = "Time (Days)", y = "Strength",
color = "Treatment (1=Reps, 2=Weight)") +
theme_minimal()Workshop: Linear Mixed Models in jamovi
1 Quick Background
- Linear Mixed Models (LMMs): LMMs extend linear regression to account for clustered (correlated) data, such as repeated measures on subjects. They include fixed effects (e.g., treatment, time) for population-level trends and random effects (e.g., random intercepts or slopes per subject) to model within-subject correlation and heterogeneity.
- Key Components:
- Fixed Effects: Coefficients for predictors like treatment (
trt) and time. - Random Effects: Typically
(1|id)for random intercepts (subject-specific baseline), or(time|id)for random slopes (individual time trends).
- Fixed Effects: Coefficients for predictors like treatment (
- Assumptions: Linearity, normality of residuals/random effects, homoscedasticity; handles missing data under MAR.
- Here: We’ll analyze strength gains over time by treatment group using the Linear Mixed Models module in Jamovi. Outcome: strength (
y); Predictor: time (days); Group:trt(1=increase reps, 2=increase weight); Clustering: subjectid.
The dataset has 37 subjects (16 in trt=1, 21 in trt=2), with up to 7 measurements (assuming time points at months 0,2,4,6,8,10,12 .
Note: Run in Jamovi. Download and save the provided stren_long.csv file (long format) in your working directory, or load it directly.
2 Install and Load Modules
Jamovi comes with core modules, but for Linear Mixed Models, ensure the “Linear Mixed Models” module is installed (part of the jamovi library).
- Open Jamovi.
- Go to the “+” (modules) button in the top-right toolbar.
- Search for “Rj - Editor” and install it if not already present.
- Search for “jReshape” and install it if not already present.
- Search for “Linear Models” and install “GAMLj3” it if not already present.
- Restart Jamovi if prompted.
- What this does:
- The Rj-Editor allows you run R code withig jamovi.
- The jReshape module allow to to go from the wide-format data to the long-format data. (not used in this example)
- The GAMLv3 has implemented the linear mixed models interface for fitting LMMs; the Regression module adds ordinary least squares for comparison.
- Expected output: Modules appear in the Analyses bar or in the “+” sign under *jamovi library”
3 Load the data
Load the long-format data directly into Jamovi.
- Open jamovi and create a new file.
- Drag and drop
stren_long.csvinto the data pane, or use File > Open. - Verify variables:
id(ID, nominal),trt(Treatment, nominal),time(Time, continuous),strength(Strength, continuous). - If needed, right-click variables to set types (e.g.,
idandtrtas Nominal).
- What this does: Loads long-format data (one row per measurement), with
timein days (0,2,4,6,8,10,12), and removes NAs. - Expected output: ~200-250 rows (some missings). Means: strength ~80-82 increasing over time; ~16 subjects trt=1, ~21 trt=2; measurements per time ~30-37.
4 Prepare the Dataset
jamovi will try to automatically idetify the type of variables in the dataset but it not always correct. In this case we need to:
- Change the type of the variables time and strength from nominal to continuous.
- And you can also add the labels to the trt categories
5 Summarise and visualise the Data
Use the Descriptives module for summaries and the Exploration module for a spaghetti plot.
- Go to Analyses > Exploration > Descriptives.
- Variables: Drag
strengthto Dependent Variables. - Plots: Check “Plot” for histogram/boxplot.
- Split by: Drag
trtto Split.
- Variables: Drag
- For the spaghetti plot we will use the module to write the code in R (there is no current plot module that can to these plots). Use the R editor as in the animated image below and copy paste the R code:
- What this does: Summarizes data by group; plots individual trajectories (lines per id) and group trends.
6 Fit a Basic Random Intercept Model
Model strength as function of time and trt, with random intercept per subject.
- Go to Analyses > Linear Models> Linear Mixed Models
Compute predicted values: In the module, check “Predicted values” under Additional options (adds column to data).
For visualization: Use the plot options in the module or export predictions.
- [Screenshot placeholder: Predicted spaghetti plot]
What this does: Fixed effects: intercept, time slope, trt effect; random: subject-specific deviation from intercept.
Expected output: Positive time coef ; trt coef ( positive for trt=2); random SD (between-subject variability). p-values from the table.
7 Compare to Ordinary Linear Model (For Illustration)
Ignore clustering with ordinary linear regression.
Go to Analyses > Regression > Linear regression.
- Dependent Variable:
strength. - Covariates:
timeandtrt.
- Dependent Variable:
Compare coefficients/SEs from the LMM output.
[Screenshot placeholder: Linear regression results table]
Expected: lm SEs smaller (underestimates uncertainty); biased p-values. LMM SEs larger, more conservative.
8 Add Treatment-Time Interaction
Allow different slopes by treatment.
In the Linear mixed models analysis (edit existing or new):
- Covariates: Add interaction by selecting
timeandtrtand clicking the * button, or manually entertime * trt. - Random effects: Keep
id(intercept only). - Options: Include “Fixed effects tests” for interaction p-value.
- Covariates: Add interaction by selecting
Compute predicted values as before.
[Screenshot placeholder: Interaction model interface and predicted plot]
What this does: Tests if time slope differs by trt (interaction term).
Expected output: Interaction p<0.05? (trt=2 faster gains); main effects adjusted. Use the table for p-values.
9 Include Random Slopes
Allow subject-specific time slopes.
Edit the interaction model:
- Random effects: Drag
timeto Random slopes (under theidgrouping). - This adds
(time | id). - Options: Check “Random effects tests” and “Model fit measures” (e.g., AIC/BIC).
- Random effects: Drag
Compute predicted values.
Compare models: View AIC/BIC in the Model fit table (lower better).
[Screenshot placeholder: Random slopes interface, results table, and predicted plot]
What this does: Random
(time | id)adds subject-specific slopes; compare fit via AIC (lower better).Expected output: Random slope SD; better fit (lower AIC) than intercept-only.
10 Model Diagnostics
Check assumptions using built-in plots.
In the Linear mixed models module (random slopes model):
- Options > Assumptions: Check “Residuals vs fitted”, “Q-Q plot”, “Histogram of residuals”.
Review the generated plots.
[Screenshot placeholder: Diagnostics plots (residuals vs fitted, QQ, histogram)]
What this does: Plots check linearity/homoscedasticity; QQ for normality.
Expected output: Points scatter around line (no patterns); QQ straight; residuals ~normal.